package com.etl.user.controller;

import com.etl.base.BaseController;
import com.xr.redis.util.CacheManager;
import com.xr.util.LocalCacheUtil;
import com.xr.util.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

/**
 * <p>
 * 验证码 前端控制器
 * </p>
 *
 * @author Arnold Yang
 * @since 2018-07-30
 */
@Controller
@RequestMapping("/vfy/code")
public class VerifyCodeController extends BaseController {

  /**
   * 生成验证码
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping("/generate")
  public void codeGenerate() throws Exception {

    String code = Utils.getStringRandom(5);
    BufferedImage image = Utils.drawImage(code);

    // 输出图片
    response.setContentType("image/png");
    response.setHeader("Cache-Control", "no-cache, no-store");
    response.setHeader("Pragma", "no-cache");
    long time = System.currentTimeMillis();
    response.setDateHeader("Last-Modified", time);
    response.setDateHeader("Date", time);
    ImageIO.write(image, "png", response.getOutputStream());
    response.flushBuffer();

    CacheManager.set("abc", code);
  }
}
